# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=('libxml2' 'libxml2-devel' 'libxml2-python')
pkgver=2.9.2
pkgrel=2
pkgdesc="XML parsing library, version 2"
arch=(i686 x86_64)
license=('MIT')
makedepends=('python2' 'python3' 'libreadline-devel' 'ncurses-devel' 'liblzma-devel' 'zlib-devel')
url="http://www.xmlsoft.org/"
source=(ftp://ftp.xmlsoft.org/${pkgname}/${pkgname}-${pkgver}.tar.gz
        http://www.w3.org/XML/Test/xmlts20130923.tar.gz
        libxml2-2.9.0-do-not-check-crc.patch
        libxml2-2.7.3-doc-install.patch
        libxml2-2.8.0_rc1-winnt.patch
        libxml2-2.9.1-msys2.patch
        libxml2-2.9.2-catalog-revert.patch)
md5sums=('9e6a9aca9d155737868b3dc5fd82f788'
         'ef8fbe0c92efd9a60d7f320e211d10b2'
         '08e92a1d05827f4152ab30bd56655fdd'
         'f55c43ccb3ec01a61c1436b0bcd1a4ca'
         '2a38e25bead58e4b4dc4550317369c90'
         '5da1ce75baca27d4fb4d7aa745e4e0b6'
         'c5f4a21e64558d01149ce2e2b8147c28')

PYTHON_SITELIB=/usr/lib/python2.7/site-packages
PYTHON_LIB=/usr/lib/python2.7

prepare() {
  cd ${srcdir}
  mv xmlconf -t ${pkgname}-${pkgver}

  cd ${pkgname}-${pkgver}

  patch -p1 -i ${srcdir}/libxml2-2.9.0-do-not-check-crc.patch
  patch -p1 -i ${srcdir}/libxml2-2.7.3-doc-install.patch
  patch -p1 -i ${srcdir}/libxml2-2.8.0_rc1-winnt.patch
  patch -p1 -i ${srcdir}/libxml2-2.9.1-msys2.patch
  patch -p1 -i ${srcdir}/libxml2-2.9.2-catalog-revert.patch

  sed -e 's|/usr/bin/python -u|/usr/bin/python2 -u|g' -e 's|/usr/bin/python$|/usr/bin/python2|g' -i python/tests/*.py

  autoreconf -fi
}

build() {
  cd ${pkgname}-${pkgver}
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${CHOST} \
    --enable-ipv6 \
    --with-history \
    --with-python=/usr/bin/python2 \
    --with-html-dir=/usr/share/doc \
    --with-html-subdir=libxml2/html
  make
  make DESTDIR=${srcdir}/dest install

  mv ${srcdir}/dest${PYTHON_SITELIB}/${pkgname/lib/msys-}mod.dll ${srcdir}/dest${PYTHON_SITELIB}/${pkgname}mod.dll
  sed -i -e "s:${pkgname/lib/msys-}mod:${PN}mod:g" ${srcdir}/dest${PYTHON_SITELIB}/${pkgname}mod.la
}

check() {
  cd ${pkgname}-${pkgver}
  make check
}

package_libxml2() {
  depends=('coreutils' 'liblzma' 'libreadline' 'ncurses' 'zlib')
  groups=('libraries')
  install=libxml2.install

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  rm -f ${pkgdir}/usr/bin/*-config
  mkdir -p ${pkgdir}/usr/share
  cp -rf ${srcdir}/dest/usr/share/man ${pkgdir}/usr/share/
  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/COPYING "${pkgdir}/usr/share/licenses/${pkgname}/COPYING"
}

package_libxml2-devel() {
  pkgdesc="Libxml2 headers and libraries"
  groups=('development')
  options=('staticlibs')
  depends=("libxml2=${pkgver}" 'libreadline-devel' 'ncurses-devel' 'liblzma-devel' 'zlib-devel')

  mkdir -p ${pkgdir}/usr/{bin,share}
  cp -f ${srcdir}/dest/usr/bin/*-config ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
  rm -rf ${pkgdir}${PYTHON_SITELIB}
  cp -rf ${srcdir}/dest/usr/share/aclocal ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/doc ${pkgdir}/usr/share/
  rm -rf ${pkgdir}/usr/share/doc/python-libxml2
  cp -rf ${srcdir}/dest/usr/share/gtk-doc ${pkgdir}/usr/share/
}

package_libxml2-python(){
  pkgdesc="Libxml2 python modules"
  depends=('libxml2')
  groups=('python-modules')
  options=('staticlibs')
  install=libxml2-python.install

  mkdir -p ${pkgdir}/${PYTHON_SITELIB}
  mkdir -p ${pkgdir}/usr/share/doc
  cp -rf ${srcdir}/dest${PYTHON_SITELIB}/* ${pkgdir}${PYTHON_SITELIB}/
  cp -rf ${srcdir}/dest/usr/share/doc/python-libxml2 ${pkgdir}/usr/share/doc/
}
